package com.xqx.thread.initial;

public class ThreadYield {
    public static void main(String[] args) {
        YieldTest thread1=new YieldTest("张三");
        YieldTest thread2=new YieldTest("李四");
        thread1.setPriority(Thread.MAX_PRIORITY);
        thread2.setPriority(Thread.MIN_PRIORITY);
        thread1.start();
        thread2.start();
    }
}
class YieldTest extends Thread{
    public YieldTest(String name){
        super(name);
    }

    @Override
    public void run() {
        for (int i = 0; i < 50; i++) {
            System.out.println(""+this.getName()+"----"+i);
            // 当i为30时，该线程就会把CPU时间让掉，让其他或者自己的线程执行（也就是谁先抢到谁执行）
            if(i==30){
                this.yield();
            }
        }
    }
}